<?php

namespace app\admin\model;

use fast\Tree;
use think\Model;
use traits\model\SoftDelete;

class Classes extends Model
{
    protected $connection = 'database.cheat';
    // 表名
    protected $table = 'classes';
    use SoftDelete;
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime='deletetime';
    
    // 追加属性
    protected $append = [
        'display_text',
    ];


    public function getDisplayList()
    {
        return ['1' => __('Display 1'),'2' => __('Display 2')];
    }
    public function getDisplayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function pclass(){
       return  $this->belongsTo('classes','pid','id',[],'LEFT')->setEagerlyType(0);

    }
    /**
     * 无限分类
     * @return array|Model
     */
    public function getParent()
    {
        // 必须将结果集转换为数组
        $ruleList = collection(Classes::where(['display' => 1])->select())->toArray();
        Tree::instance()->init($ruleList);
        $rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'names');
        $ruledata = [0 => '选择分类'];
        foreach ($rulelist as $k => &$v)
        {
            $ruledata[$v['id']] = $v['names'];
        }
        return $ruledata;
    }


}
